﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Gift.DAL;
using Gift.Model;

namespace WebSite.cn
{
    public partial class inputCard : System.Web.UI.UserControl
    {
        readonly MemberAccount theMemberAccount = new MemberAccount();
        readonly Card theCard = new Card();
        readonly Member theMember = new Member();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (new BasePage().LoginUserMember == null)
                {
                    this.btnAdd.CausesValidation = false;
                }
                else
                {
                    this.btnAdd.CausesValidation = true;
                }
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            BasePage thePage = new BasePage();

            if (thePage.LoginUserMember == null)
            {
                base.Response.Write("<script language=javascript>window.location.href='login.aspx';</script>");
            }

            if (string.Compare(txtValidate.Text.Trim(), Session["checkCode"].ToString()) != 0)
            {
                txtValidate.Text = string.Empty;
                // this.lblMessage.Text = "验证码不正确！请重新输入！";
                Response.Write(string.Format("<script>alert('验证码不正确！请重新输入！');window.location.href='{0}';</script>",this.Request.Url));
                return;
            }
            txtValidate.Text = string.Empty;
            dbCard oCard;
            oCard = theCard.GetModel(this.txtCardCode.Text.Trim(), this.txtCardPassword.Text.Trim());
            if (oCard != null)
            {
                if (oCard.states == 0)
                {
                    this.lblMessage.Text = "该卡还未激活，请先开卡后才能使用！";
                }
                else if (oCard.states == 2)
                {
                    this.lblMessage.Text = "该卡已被使用！";
                }
                else if (oCard.states == 3)
                {
                    this.lblMessage.Text = "该卡已过了兑换有效日期！";
                }
                else if (oCard.states == 4)
                {
                    this.lblMessage.Text = "该卡还未激活,请先开卡后才能使用！";
                }
                else if (oCard.StartTime > DateTime.Now.Date)
                {
                    this.lblMessage.Text = "该卡还未生效！";
                }
                else if (oCard.lasttime < DateTime.Now.Date)
                {

                    this.lblMessage.Text = "该卡已过了兑换有效日期！";

                }
                else if (DateTime.Now.Date > oCard.endtime && DateTime.Now.Date <= oCard.lasttime)
                {

                    if (oCard.states == 1)
                    {
                        theMemberAccount.ConvertToAccount(int.Parse(thePage.LoginUserMember.MemberId), oCard.cardid);
                        Response.Write(string.Format("<script>alert('充值成功！');window.location.href='{0}';</script>", this.Request.Url));
                        base.Response.End();

                    }
                    else
                    {
                        this.lblMessage.Text = "充值失败，请联系客服！";
                    }

                }
                else if (DateTime.Now.Date <= oCard.endtime)
                {
                    if (thePage.LoginUserMember.DicUserCardList == null
                        || !thePage.LoginUserMember.DicUserCardList.ContainsKey(oCard.cardid))
                    {

                        if (thePage.LoginUserMember.DicUserCardList == null)
                        {
                            thePage.LoginUserMember.DicUserCardList = new Dictionary<int, dbCard>();
                        }
                        thePage.LoginUserMember.DicUserCardList[oCard.cardid] = oCard;
                    }
                    else if (thePage.LoginUserMember.DicUserCardList.ContainsKey(oCard.cardid))
                    {
                        Response.Write(string.Format("<script>alert('该卡已充值！');window.location.href='{0}';</script>", this.Request.Url));

                        base.Response.End();
                       
                    }
                    Response.Write(string.Format("<script>alert('充值成功！');window.location.href='{0}';</script>", this.Request.Url));
                
                    base.Response.End();
                }
            }
            else
            {
                this.lblMessage.Text = "卡号或密码不正确！";
            }
        }
    }
}